import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.ValidationException;

import bean.User;

public class No1_22 {
	private static final String CSV_EXTENSION = "csv";
	private static final String INVALIV_FILE_MESSAGE = "Invalid file extension";
	public static void main(String[] args) throws IOException {
		try {
			File file = new File("file/file.csv");
			String name = file.getName();
			int k = name.lastIndexOf(".");
		    String ext = null;
		    if(k != -1)
		    	ext = name.substring(k + 1, name.length());
//		    System.out.println(ext);
		    
		    if(!ext.equalsIgnoreCase(CSV_EXTENSION)){
				throw new ValidationException(INVALIV_FILE_MESSAGE);
		    }
		
			ClassLoader loader = Thread.currentThread().getContextClassLoader();
			InputStream in = loader.getResourceAsStream(file.getPath());
			
			Reader reader = new InputStreamReader(in, "utf-8");
			BufferedReader CSVFile = new BufferedReader(reader);
	
			List<User> userList = new ArrayList<User>();
			String dataRow = CSVFile.readLine();
			while (dataRow != null) {
				String[] dataArray = dataRow.split(",");
				
				User user = new User();
				user.setFirstName(dataArray[0]);
				user.setLastName(dataArray[1]);
				user.setPhoneNumber(dataArray[2]);
				
				userList.add(user);
				
				dataRow = CSVFile.readLine(); 
			}
			
			CSVFile.close();
			
			for (User user : userList) {
				System.out.println(user);
			}
		
		} catch (ValidationException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
	}
}
